package cn.ffcs.uec.selfstaff.component;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModel;

import cn.ffcs.uec.selfstaff.component.bean.UecSelfWorkListboxBean;
import cn.ffcs.uec.selfstaff.constant.UecSelfStaffConstants;
import cn.ffcs.uec.staff.manager.UecWorkManager;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uec.staff.model.UecWork;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import lombok.Getter;
import lombok.Setter;

/**
 * @Title:履历记录CRUD插件
 * @Description:
 * @Copyright: FFCS（C） 2017
 * @Company: FFCS
 * @author Wang Yiren
 * @Version 0.0.1 date:2017年11月27日
 * @since JDK1.6
 */
@Controller
@Scope("prototype")
public class UecSelfWorkListboxExt extends Div implements IdSpace {

    private static final long serialVersionUID = -5811398149406451671L;

    /**
     * 人员基本信息功能 查询出的实体对象
     */
    private UecStaffInfo uecStaffInfo;

    /**
     * 对应页面上的bean
     */
    private UecSelfWorkListboxBean bean = new UecSelfWorkListboxBean();

    /**
     * 人员语言能力实体
     */
    private UecWork uecWork;

    /**
     * 人员语言能力查询实体
     */
    // private UecWork qryUecWork;
    private UecWorkManager uecWorkManager = (UecWorkManager) ApplicationContextUtil.getBean("uecWorkManager");

    @Getter
    @Setter
    private IPortletInfoProvider portletInfoProvider;

    public UecSelfWorkListboxExt() {
        Executions.createComponents(UecSelfStaffConstants.UEC_WORK_EXT_LISTBOX_ZUL, this, null);
        Components.wireVariables(this, bean);
        Components.addForwards(this, this, '$');
        this.addForward(UecSelfStaffConstants.ON_UEC_WORK_QUERY, this,
                UecSelfStaffConstants.ON_UEC_WORK_QUERY_RESPONSE);
        this.addForward(UecSelfStaffConstants.ON_CLEAN_UEC_WORK, this,
                UecSelfStaffConstants.ON_CLEAN_UEC_WORK_RESPONSE);
    }

    /**
     * 初始化
     */
    public void onCreate() {
    }

    /**
     * 选择查询记录
     * 
     * @throws Exception
     */
    public void onSelectUecWorkList() throws Exception {
        if (this.bean.getUecWorkListbox().getSelectedCount() > 0) {
            uecWork = (UecWork) bean.getUecWorkListbox().getSelectedItem().getValue();
        }
    }

    /**
     * 人员基本信息功能，抛出事件， 事件监听
     * 
     * @param event
     */
    public void onUecWorkQueryResponse(ForwardEvent event) {
        uecStaffInfo = (UecStaffInfo) event.getOrigin().getData();
        if (null != uecStaffInfo) {
            uecWork = new UecWork();
            if (!StrUtil.isNullOrEmpty(uecStaffInfo.getPkPsndoc())
                    && !StrUtil.isNullOrEmpty(uecStaffInfo.getStaffId())) {
                uecWork.setPkPsndoc(uecStaffInfo.getPkPsndoc());
            } else {
                uecWork.setStaffId(uecStaffInfo.getStaffId());
            }
            this.onQueryUecWork();
        }
    }

    public void onCleanUecWorkResponse(ForwardEvent event) {
        clearVariable();
        uecStaffInfo = null;
        ListboxUtils.clearListbox(this.bean.getUecWorkListbox());
        this.bean.getUecWorkListboxPaging().setTotalSize(1);
    }

    /**
     * 查询按钮
     */
    public void onQueryUecWork() {
        this.bean.getUecWorkListboxPaging().setActivePage(0);
        this.onQueryUecWorkList();
    }

    /**
     * 翻页
     */
    public void onUecWorkListboxPaging() {
        this.onQueryUecWorkList();
    }

    /**
     * 清除变量
     */
    public void clearVariable() {
        uecWork = null;
    }

    /**
     * 查询
     */
    public void onQueryUecWorkList() {
        if (null != uecWork) {
            PageInfo pageInfo = uecWorkManager.queryPageInfoByUecWork(uecWork,
                    this.bean.getUecWorkListboxPaging().getActivePage() + 1,
                    this.bean.getUecWorkListboxPaging().getPageSize());
            ListModel list = new BindingListModelList(pageInfo.getDataList(), true);
            this.bean.getUecWorkListbox().setModel(list);
            this.bean.getUecWorkListboxPaging().setTotalSize(pageInfo.getTotalCount());
        }
    }
}
